package com.example.mydemo.schedule;
import com.example.mydemo.entity.domain.AttendanceStatDTO;
import com.example.mydemo.service.impl.AttendanceService;
import com.example.mydemo.service.impl.ClockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
public class AttendanceSchedule{
    @Autowired
    private AttendanceService attendanceService;
    @Autowired
    private ClockService clockService;
     @Scheduled(fixedRate = 1000*600) // 每3秒执行一次
     public void performTask() throws ParseException, InterruptedException {
         System.out.println("开始执行定时任务：统计考勤数据");
         // 获取当前日期（基于系统默认时区）
         for (int i = 1; i < 5; i++){
             LocalDate currentDate = LocalDate.now();
             // 减去 1 个月（自动处理跨年，如 2024-01 → 2023-12）
             LocalDate lastMonth = currentDate.minusMonths(i);
             // 格式化为 yyyy-MM（如 2023-12）
             DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
             String lastMonthStr = lastMonth.format(formatter);
             List<AttendanceStatDTO> AttendanceList = attendanceService.getAttendanceStat(lastMonthStr);
             attendanceService.batchInsertOrUpdate(AttendanceList);
             System.out.println("成功执行定时任务：统计考勤数据的月份：" + lastMonthStr);
         }
     }

}
